package StudyCode.summary.mycollection;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertsAListOfObjectsIntoAMap {
	@SuppressWarnings("unchecked")
	public static <K, V> Map<K, V> asMap(Object... keyValPair) {
		Map<K, V> map = new HashMap<K, V>();

		if (keyValPair.length % 2 != 0) {
			throw new IllegalArgumentException(
					"Keys 跟 values 必须一一匹配.");
		}

		for (int i = 0; i < keyValPair.length; i += 2) {
			map.put((K) keyValPair[i], (V) keyValPair[i + 1]);
		}

		return Collections.unmodifiableMap(map);
	}
}
